package com.jamal2362.apktool.preference;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import androidx.appcompat.app.AlertDialog;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.util.FilePicker;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.Settings;
import com.jamal2362.apktool.util.TextUtil;

public class SelectPreference extends CustomPreference
{
	private EditText dirPath;
	private ImageButton openPicker;

	public SelectPreference(Context ctx, AttributeSet a)
	{
		super(ctx, a);
	}

	@Override
	protected void onClick()
	{
		final Context context = getContext();
		final LayoutInflater inflater = LayoutInflater.from(context);
		final View view = inflater.inflate(R.layout.select_dir, null);
		final IconFactory icFactory = new IconFactory(context);
		final AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
		mBuilder.setView(view);
		mBuilder.setTitle(R.string.output_directory);
		mBuilder.setNegativeButton(R.string.cancel, mClick);
		mBuilder.setPositiveButton(R.string.save, mClick);
		int fcolor = (!Settings.lightTheme ? Color.BLACK : Color.DKGRAY);
		Bitmap folder = icFactory.makeIcon(fcolor, R.drawable.ic_folder, 24, 16, 0);
		dirPath = view.findViewById(R.id.selectdirEditText1);
		SharedPreferences sp = getSharedPreferences();
		String path = sp.getString("output_directory", "");
		dirPath.setText(path);
		openPicker = view.findViewById(R.id.selectdirImageButton1);
		openPicker.setImageBitmap(folder);
		openPicker.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View p1)
				{
					final FilePicker picker = new FilePicker(context);
					final Runnable cback = new Runnable()
					{
						@Override
						public void run()
						{
							dirPath.setText(picker.getPath());
						}
					};
					picker.setCallback(cback);
					picker.setPath(Environment.getExternalStorageDirectory().getAbsolutePath());
					AlertDialog.Builder pbuilder = new AlertDialog.Builder(context);
					pbuilder.setTitle(R.string.mselect);
					pbuilder.setView(picker);
					pbuilder.setPositiveButton(R.string.mselect, new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(DialogInterface p1, int p2)
							{
								cback.run();
								p1.dismiss();
							}
						});
					pbuilder.setNegativeButton(R.string.cancel, null);
					AlertDialog dialog = pbuilder.create();
					picker.setDialog(dialog);
					dialog.show();
				}
			});
		mBuilder.create().show();
	}

	private DialogInterface.OnClickListener mClick = new DialogInterface.OnClickListener()
	{
		@Override
		public void onClick(DialogInterface dialog, int which)
		{
			if (which == DialogInterface.BUTTON_POSITIVE)
			{
				SharedPreferences.Editor editor = getSharedPreferences().edit();
				editor.putString("output_directory", dirPath.getText().toString());
				editor.commit();
			}
			else if (which == DialogInterface.BUTTON_NEUTRAL)
			{
				SharedPreferences.Editor editor = getSharedPreferences().edit();
				editor.putString("output_directory", "");
				editor.commit();
			}
		}
	};
}
